package com.itqf.controller;

import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description:  转发和重定向 不走视图解析器(前缀后缀)
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/10/10
 * @Time: 上午9:40
 */
@Controller
@SessionAttributes({"name"}) //把model(map)中的name变量存储到session域中
//@RequestMapping("/second") //user   menu  order
public class SecondController {

    @RequestMapping("/second/test1")
    public   String test1(HttpServletRequest request){
        System.out.println("/second/test1----");
        //转发
        request.setAttribute("name","测试转发");
        return "forward:/second/test2";//找另一个请求
        //return "forward:register.jsp";//页面
    }


    @RequestMapping("/second/test2")
    public   String test2(HttpServletRequest request){
        System.out.println("/second/test2----");
        //转发
        //System.out.println("name="+request.getAttribute("name"));

        request.setAttribute("name","request作用域的值");
        User user = new User();
        user.setName("李四");
        user.setId(1);
        request.getSession().setAttribute("user",user);

        return "forward:/message.jsp";
    }

    @RequestMapping("/second/test3")
    public  String   test3(HttpServletRequest request){
        System.out.println("/second/test3----");
        request.setAttribute("name","测试重定向");
        return  "redirect:/second/test4";
    }

    @RequestMapping("/second/test4")
    public  String   test4(HttpServletRequest request){
        System.out.println("/second/test4----");
        System.out.println(request.getAttribute("name"));
        return  "redirect:/message.jsp";
    }


    @RequestMapping("/second/test5")
    public  String   test5(){
        System.out.println("/second/test5----");
        return  "redirect:/first/hello";
    }


   // Model/ModelAndView 存储到  request作用域
    @RequestMapping("/testModel1")
    public   String  test6(Model model){
        model.addAttribute("name","model中存储的值");
        return "forward:/message.jsp";
    }

    @RequestMapping("/testModel2")
    public ModelAndView test7(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name","modelAndView中存储的值");
        modelAndView.setViewName("forward:/message.jsp");

        return  modelAndView;
    }


    @RequestMapping("/testModel3")
    public   String  test6(SessionStatus  sessionStatus){
        //allowing for  cleanup of session attributes.
        sessionStatus.setComplete();//清空通过@sessionAttributes注解存储到session的值
        return "forward:/message.jsp";
    }

   // @SessionAttribute("name")单个键值对  用在方法的参数前
    //获取到session中存储的值  并且赋值给参数变量
    @RequestMapping("/testModel4")
    public   String  test6(@SessionAttribute("name") String  name,Model model){
        model.addAttribute("name",name);
        return "forward:/message.jsp";
    }


    @RequestMapping("/testModel5")
    public   String  test7(Map map){

        map.put("name","map中");

        return "forward:/message.jsp";
    }





}
